package com.meilishuo.message.service.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.meilishuo.message.dao.ThinkOrderdetailMapper;
import com.meilishuo.message.dao.ThinkOrderdetailgoodsMapper;
import com.meilishuo.message.entity.data.ThinkOrderdetail;
import com.meilishuo.message.entity.data.ThinkOrderdetailgoods;
import com.meilishuo.message.service.SynOrderDetailService;
import com.meilishuo.message.util.entity.Address;
import com.meilishuo.message.util.entity.Prop;
import com.meilishuo.message.util.entity.Order.OrderList;
import com.meilishuo.message.util.entity.Order.OrderListGoods;
import com.meilishuo.message.util.entity.Order.OrderListOrder;
import com.meilishuo.message.util.entity.Order.OrderRequest;
import com.meilishuo.message.util.entity.Order.OrderService;
import com.meilishuo.message.util.request.OrderListRequest;
import com.meilishuo.message.util.respon.OrderListResponse;
@Service("synOrderDetailService")
public class SynOrderDetailServiceImpl implements SynOrderDetailService {
	
	@Autowired
	ThinkOrderdetailMapper thinkOrderDetailMapper;
	@Autowired
	ThinkOrderdetailgoodsMapper thinkOrderDetailGoodsMapper;
	
	@Override
	public void insertOrderDetail(OrderListOrder order,Address address,List<OrderService> orderServices) {
		ThinkOrderdetail orderdetail=new ThinkOrderdetail();
		orderdetail.setOrderId(order.getOrder_id());
		orderdetail.setAddress(address.getAddress());
		orderdetail.setBuyerNickname(order.getBuyer_nickname());
		orderdetail.setCity(address.getCity());
		orderdetail.setCtime(order.getCtime());
		orderdetail.setDistrict(address.getDistrict());
		orderdetail.setExpressCompany(order.getExpress_company());
		orderdetail.setExpressId(order.getExpress_id());
		orderdetail.setExpressPrice(order.getExpress_price());
		orderdetail.setLastStatusTime(order.getLast_status_time());
		orderdetail.setNickname(address.getNickname());
		orderdetail.setOrderId(order.getOrder_id());
		orderdetail.setPayTime(order.getPay_time());
		orderdetail.setPayTimeOut(order.getPay_time_out());
		orderdetail.setPhone(address.getPhone());
		orderdetail.setPostcode(address.getPostcode());
		orderdetail.setProvince(address.getProvince());
		orderdetail.setReceiveTime(order.getReceive_time());
		orderdetail.setReceviceTimeOut(order.getReceive_time_out());
		orderdetail.setSendTime(order.getSend_time());
		for (OrderService orderService : orderServices) {
			orderdetail.setServiceComment(orderService.getComment());
			orderdetail.setServiceCtime(orderService.getServiceCtime());
			orderdetail.setServiceNickname(orderService.getService_nickname());
		}
		orderdetail.setServiceTimeOut(order.getService_time_out());
		orderdetail.setStatusText(order.getStatus_text());
		orderdetail.setStreet(address.getStreet());
		orderdetail.setTotalPrice(order.getTotal_price());
		thinkOrderDetailMapper.insert(orderdetail);
	}
	
	 public void sycOrderList(OrderListRequest request,String session){
	    	
	    	OrderListResponse orderListResponse=new OrderListResponse();
	    	OrderRequest orderRequest=new OrderRequest();
	    	List<OrderList> orderLists=new ArrayList<OrderList>();
	    	String result= orderListResponse.getOrderList(request, session);
	    	String order_id="";
	    	if (result != "false") {
				orderRequest=JSON.parseObject(result,OrderRequest.class);
				orderLists=orderRequest.getInfo();
			}
	    	double total_num=orderRequest.getTotal_num();
	    	if (total_num <= 100) {
	    		for (OrderList orderList : orderLists) {
					OrderListOrder order= orderList.getOrder();
					Address address=orderList.getAddress();
					List<OrderService> services=orderList.getService();
					insertOrderDetail(order, address, services);
					order_id=order.getOrder_id();
					Date ctime=order.getCtime();
					Date pay_time=order.getPay_time();
					List<OrderListGoods> orderListGoods=orderList.getGoods();
					for (OrderListGoods orderListGoods2 : orderListGoods) {
						insertGoods(orderListGoods2, order_id, ctime, pay_time);
					}
				}
			}else {
				int pageSize=(int)Math.ceil(total_num/100);
				for (int i = 0; i < pageSize; i++) {
					request.setPage(i);
					result=orderListResponse.getOrderList(request, session);
					if (result != "false") {
						orderRequest=JSON.parseObject(result,OrderRequest.class);
						orderLists=orderRequest.getInfo();
						for (OrderList orderList : orderLists) {
							OrderListOrder order=orderList.getOrder();
							Address address=orderList.getAddress();
							List<OrderService> services=orderList.getService();
							order_id=order.getOrder_id();
							insertOrderDetail(order, address, services);
							Date ctime=order.getCtime();
							Date pay_time=order.getPay_time();
							List<OrderListGoods> orderListGoods=orderList.getGoods();
							for (OrderListGoods orderListGoods2 : orderListGoods) {
								insertGoods(orderListGoods2, order_id, ctime, pay_time);
							}
						}
					}
				}
			}
	    }

	@Override
	public void insertGoods(OrderListGoods goods, String order_id,Date ctime,Date pay_time) {
		// TODO Auto-generated method stub
		ThinkOrderdetailgoods orderdetailgoods=new ThinkOrderdetailgoods();
		orderdetailgoods.setAmount(goods.getAmount());
		orderdetailgoods.setCtime(ctime);
		orderdetailgoods.setDeliverStatus(goods.getDeliver_status());
		orderdetailgoods.setGoodsCode(goods.getGoods_code());
		orderdetailgoods.setGoodsId(goods.getGoods_id());
		orderdetailgoods.setGoodsImg(goods.getGoods_img());
		orderdetailgoods.setGoodsNo(goods.getGoods_no());
		orderdetailgoods.setGoodsPlatformCoupon(goods.getPlat_coupon());
		orderdetailgoods.setGoodsShopCoupon(goods.getShop_coupon());
		orderdetailgoods.setGoodsTitle(goods.getGoods_title());
		orderdetailgoods.setMid(goods.getMid());
		orderdetailgoods.setOrderId(order_id);
		orderdetailgoods.setPayTime(pay_time);
		orderdetailgoods.setPrice(goods.getPrice());
		
		List<Prop> props= goods.getProp();
		String propString="";
		for (Prop prop : props) {
			propString=propString+prop.getName()+":"+prop.getValue()+"#";
		}
		
		orderdetailgoods.setProp(propString);
		orderdetailgoods.setRefundStatusText(goods.getRefund_status_text());
		orderdetailgoods.setSkuId(goods.getSku_id());
		orderdetailgoods.setTwitterId(goods.getTwitter_id());
		thinkOrderDetailGoodsMapper.insert(orderdetailgoods);
	}
}
